package com.example.springboot.controller;


import com.example.springboot.entity.IncorrectQuestion;
import com.example.springboot.entity.Res;
import com.example.springboot.entity.vo.IncorrectQuestionVo;
import com.example.springboot.entity.vo.MistakeStatisticsVo;
import com.example.springboot.service.Impl.IncorrectQuestionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/incorrectQuestions")
public class IncorrectQuestionController {

    @Autowired
    private IncorrectQuestionServiceImpl incorrectQuestionService;

    @PostMapping
    public Res<Void> addIncorrectQuestion(@RequestBody IncorrectQuestion incorrectQuestion) {
        try {
            System.out.println(incorrectQuestion);
            if (incorrectQuestion.getUserId() == null || incorrectQuestion.getQuestionId() ==null) {
                return new Res<Void>().code(400).msg("必填字段不能为空");
            }
            incorrectQuestionService.addIncorrectQuestion(incorrectQuestion);
            return new Res<Void>().code(200).msg("错题添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Res<Void>().code(500).msg("服务错误");
        }
    }

    @GetMapping("/user/{userId}")
    public Res<List<IncorrectQuestionVo>> getIncorrectQuestionsByUserId(
            @PathVariable int userId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(defaultValue = "1") int pageNum,  // Default page 1
            @RequestParam(defaultValue = "10") int pageSize  // Default size 10
    ) {
        try {
            Timestamp start = (startTime != null && !startTime.isEmpty()) ? Timestamp.valueOf(startTime) : null;
            Timestamp end = (endTime != null && !endTime.isEmpty()) ? Timestamp.valueOf(endTime) : null;
            List<IncorrectQuestionVo> incorrectQuestions = incorrectQuestionService.getIncorrectQuestionsByUserId(
                    userId, start, end, pageNum, pageSize);

            if (incorrectQuestions != null && !incorrectQuestions.isEmpty()) {
                return new Res<List<IncorrectQuestionVo>>().code(200).msg("查询成功").data(incorrectQuestions);
            } else {
                return new Res<List<IncorrectQuestionVo>>().code(404).msg("未找到错题");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Res<List<IncorrectQuestionVo>>().code(500).msg("服务错误");
        }
    }
    @GetMapping("/statistics/{userId}")
    public Res<MistakeStatisticsVo> getMistakeStatisticsVo(@PathVariable int userId) {
        try {
            MistakeStatisticsVo mistakeStatisticsVo = incorrectQuestionService.getMistakeStatisticsVo(userId);
            if (mistakeStatisticsVo != null)
                return new Res<MistakeStatisticsVo>().code(200).msg("查询成功").data(mistakeStatisticsVo);
            else
                return new Res<MistakeStatisticsVo>().code(404).msg("未找到错题");
        } catch (Exception e) {
            e.printStackTrace();
            return new Res<MistakeStatisticsVo>().code(500).msg("服务错误");
        }
    }


    @DeleteMapping("/{id}")
    public Res<Void> deleteIncorrectQuestion(@PathVariable int id) {
        try {
            incorrectQuestionService.deleteIncorrectQuestion(id);
            return new Res<Void>().code(200).msg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Res<Void>().code(500).msg("服务错误");
        }
    }
}
